clear all;

cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','nmkt','period_trans','pdf_B_forward',...
    'Y','frac_entre','labor_demand_stat','frac_credit','income_entre','income_worker','grid_income',...
    'cash','deposit');
load('code_main_model\6_implied_w_r\input\comm_loc_model.mat','comm_loc_model')
cd 'code_figure_table\maintext\Figure 13';
load('input.mat','d','distance')
size=sum(pdf_B_forward,[3,4,5]);

wealth=cash+deposit;

highdist_mkt=find(d>median(d(d>0)));

% A
frac_highdist(1:10,1)=0;
for i_t=1:10
    new_branch=find(comm_loc_model(:,1)==0 & comm_loc_model(:,i_t+1)==1);
    frac_highdist(i_t)=length(intersect(new_branch,highdist_mkt))/length(new_branch);
end

% BCDE
w_highdist(1:11,1)=0;
output_highdist(1:11,1)=0;
deposit_highdist(1:11,1)=0;
cash_highdist(1:11,1)=0;
entre_highdist(1:11,1)=0;
for i_t=1:11
    w_highdist(i_t)=sum(wealth(i_t,highdist_mkt))/sum(size(i_t,highdist_mkt));
    output_highdist(i_t)=sum(Y(i_t,highdist_mkt))/sum(size(i_t,highdist_mkt));
    deposit_highdist(i_t)=sum(deposit(i_t,highdist_mkt))/sum(size(i_t,highdist_mkt));
    cash_highdist(i_t)=sum(cash(i_t,highdist_mkt))/sum(size(i_t,highdist_mkt));
    entre_highdist(i_t)=sum(frac_entre(i_t,highdist_mkt))/sum(size(i_t,highdist_mkt));
end

w_highdist=w_highdist./w_highdist(1);
output_highdist=output_highdist./output_highdist(1);
dc_highdist=deposit_highdist./cash_highdist;

% F
T=11;
income=income_entre+income_worker;

income_economy=squeeze(sum(income,2));
gini1(1:T,1)=0;
for t=1:T
    gini1(t)=gini(1e10.*income_economy(t,:),grid_income,false);
end
income_gini=gini1;

save('baseline.mat','frac_highdist','dc_highdist','w_highdist','entre_highdist','output_highdist','income_gini');
% save('s1.mat','frac_highdist','dc_highdist','w_highdist','entre_highdist','output_highdist','income_gini');
% save('s2.mat','frac_highdist','dc_highdist','w_highdist','entre_highdist','output_highdist','income_gini');